//
//  SearchGuanJianGongXuViewController.m
//  MyProject
//
//  Created by Pei on 16/7/31.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "SearchGuanJianGongXuViewController.h"
#import "IHRefreshTableView.h"
#import "CheckAndAcceptCell.h"
#import "CheckDetailViewController.h"
#import "MultiSelectTableViewCell.h"
#import "NSMutableArray+Additions.h"
#import "ShiGongStatusViewController.h"
#import "SelectCompanyAndProjectViewController.h"
#import "UserInfo.h"
#import "GongXuDetailViewController.h"

@interface SearchGuanJianGongXuViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    IHRefreshTableView *_myTable;
    UIView *_headerView;
    
    UIButton *_rightBtn;
    
    UIControl *_addControl;
    UIView *_addView;
    
    NSString *_currentAction;
    
    //NSDictionary *firstDic;
    //NSDictionary *secondDic;
    
    int status;
    
    UILabel *lbl0;
    UILabel *lbl1;
    UILabel *lbl2;
    
    NSArray *_dataArray;
    UIView *backView;
    
    NSDictionary *projecrDic;
    //int status;
    
    NSString *keywordStr;
    
    UITextField *inputText;
}

@end

@implementation SearchGuanJianGongXuViewController

- (id)initWithProject:(NSDictionary*)dic status:(int)sta{
    self = [super self];
    if (self) {
        projecrDic = dic;
        status = sta;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //status = 0;
    
    keywordStr = @"";
    
    _rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 22)];
    //[rightBtn setBackgroundImage:[UIImage imageNamed:@"02edit"] forState:UIControlStateNormal];
    //[rightBtn setBackgroundImage:[UIImage imageNamed:@"02edit"] forState:UIControlStateHighlighted];
    [_rightBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [_rightBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    _rightBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [_rightBtn addTarget:self action:@selector(rightBtnAcion) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];
    
    
    backView = [[UIView alloc] init];
    backView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, 40.0f);
    backView.backgroundColor = kWhiteColor;
    [self.view addSubview:backView];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(0.0f, 39.5f, kDeviceWidth, 0.5f);
    lineView.backgroundColor = kGreenColor;
    [backView addSubview:lineView];
    
    inputText = [[UITextField alloc] init];
    inputText.frame = CGRectMake(10.0f, 10.0f, kDeviceWidth-20.0f, 20.0f);
    inputText.backgroundColor = [UIColor clearColor];
    inputText.placeholder = @"请输入关键字";
    [backView addSubview:inputText];
    
    
    [self initTableView];

}

- (void)getProjectStateData{
    [[YMNetLib getInstance] connectWithAction:Dictionary_Type_API method:@"GET" withContent:@"DicType=ProjectState" withBody:nil withDelegate:self];
    _currentAction = @"Dic_ProjectState";
}

- (void)getCompanyListData{
    //Get_Company_List
    [[YMNetLib getInstance] connectWithAction:Get_Company_List method:@"GET" withContent:[NSString stringWithFormat:@"UserID=%@",[[UserInfo getInstance] getUserID]] withBody:nil withDelegate:self];
    _currentAction = Get_Company_List;
}

- (void)getProjectListData:(NSString*)OrgCode{
    //Get_Project_List
    [[YMNetLib getInstance] connectWithAction:Get_Project_List method:@"GET" withContent:[NSString stringWithFormat:@"orgCode=%@", OrgCode] withBody:nil withDelegate:self];
    _currentAction = Get_Project_List;
}

- (void)getData{
    keywordStr = inputText.text;
    if (keywordStr != nil && keywordStr.length > 0) {
        keywordStr = inputText.text;
    }else{
        keywordStr = @"";
    }
    //Get_Important_Working_List
    [[YMNetLib getInstance] connectWithAction:Get_Important_Working_List method:@"GET" withContent:[NSString stringWithFormat:@"UserID=%@&projectId=%d&Keyword=%@&SGStatus=%d&Status=%@&pagesize=%@&pageindex=%@",[[UserInfo getInstance] getUserID], [[projecrDic valueForKey:@"ProjectID"] intValue], keywordStr, status,@"", @"10", @"1"] withBody:nil withDelegate:self];
    _currentAction = Get_Important_Working_List;
}

- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([_currentAction isEqualToString:Get_Important_Working_List]){
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            _dataArray = [body objectForKey:@"Items"];
            [_myTable reloadData];
            
            
            NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:Dic_ProjectState];
            if (tempArray != nil && tempArray.count > 0) {
            }else{
                [self getProjectStateData];
            }
        }
        //Items
        
    }else if ([_currentAction isEqualToString:Get_Important_Working_List]){
        
    }else if ([_currentAction isEqualToString:@"Dic_ProjectState"])
    {
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1)
        {
            [[NSUserDefaults standardUserDefaults] setObject:[body valueForKey:@"DicList"] forKey:Dic_ProjectState];
            [_myTable reloadData];
            
            
        }
    }else{
        
    }
}


- (void)rightBtnAcion
{
    /*
    [_myTable setEditing:!_myTable.editing animated:YES];
    
    _addControl.hidden = YES;
    _addView.hidden = NO;
    
    [self.navigationItem setRightBarButtonItem:nil];
    */
    [self getData];
}

- (void)addControlAction
{
    
}

- (void)btnAction:(UIButton*)sender
{
    switch (sender.tag) {
        case 116:
        {
            [_myTable setEditing:!_myTable.editing animated:YES];
            
            _addView.hidden = YES;
            _addControl.hidden = NO;
            
            [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];
        }
            break;
            
        default:
            break;
    }
}

- (void)initTableView
{
    CGRect frame = CGRectMake(0, 40.0f, kDeviceWidth, self.view.height-40);
    _myTable = [[IHRefreshTableView alloc] initWithFrame:frame refreshType:-1 andAutoLoad:NO];
    _myTable.delegate = self;
    _myTable.dataSource = self;
    _myTable.footer.hidden = YES;
    _myTable.header.hidden = YES;
    _myTable.allowsSelectionDuringEditing = YES;
    _myTable.backgroundColor = kDefaultBackgroundColor;
    [self.view addSubview:_myTable];
}


#pragma mark  ************************* UITableViewDataSource **************************************
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"MultiSelectTableViewCell";
    CheckAndAcceptCell *cell = (CheckAndAcceptCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[CheckAndAcceptCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDictionary *dd = [_dataArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell showWithData:dd];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}



#pragma mark -
#pragma mark Table view delegate

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dd = [_dataArray objectAtIndex:indexPath.row];
    
        if ([self.delegate respondsToSelector:@selector(passDic:)]) {
            [self.delegate passDic:dd];
        }
        [self.navigationController popViewControllerAnimated:YES];
}

@end
